<?php
global $Config;
require_once($Config->WebCMSRoot.'Base/Control.php');
require_once($Config->WebCMSRoot.'Views/Calendar.php');

/* TODO:
 - move parameters to page or proxy-control
*/

Class Calendar extends BaseControl
{
	var $view;
	var $calendarURL;
	var $count;
	//var $blogUser;
	//var $blogPassword;

	function Calendar($parameters, &$page)
	{
//print('<pre>'.var_export($parameters, true).'</pre>');
//print('<pre>'.var_export($this, true).'</pre>');exit;
		$this->calendarURL = "http://www.google.com/calendar/feeds/familieboden%40gmail.com/public/basic";

		$this->view = new CalendarView();

		parent::BaseControl($parameters, &$page);
//print('<pre>'.var_export($this, true).'</pre>');exit;
	}

	function Execute()
	{
//print('<pre>'.var_export($this, true).'</pre>');exit;

		switch ($this->GetParameter('show'))
		{
			case 'upcoming':
				$posts = $this->GetUpcoming($this->count);
				$this->view->PrintUpcomming(&$posts, $this->error);
				//$posts = $this->GetFull();
				//$this->view->PrintIndex(&$posts);  // Messes up HTML
				break;
		}

		parent::Execute();
	}

	function ValidateArguments($args)
	{
//print('<pre>'.var_export($this, true).'</pre>');exit;
		$valid = false;
		
		switch ($this->GetParameter('show'))
		{
			case 'upcoming':
				$valid = (count($args) == 2);
				break;
		}

		return $valid;
	}
	
	function GetUpcoming($count)
	{
		return $this->GetCalendarRequest($this->calendarURL);
	}
	
	function GetCalendarRequest($url)
	{
		$ch = curl_init();

		// set URL and other appropriate options
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

		// grab URL and pass it to the browser
		$response = curl_exec($ch);
		$this->error = array(curl_errno($ch), curl_error($ch));

		// close curl resource, and free up system resources
		curl_close($ch);
		
		return $response;
	}
}
?>